package com.skychyn;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class SetupActivity extends Activity {

	CheckBox cbUseProxy;
	EditText etIP;
	EditText etPort;
	EditText etURL;

	String url;
	String ip;
	int port;
	boolean useProxy;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.settings_layout);

		// Restore preferences
		SharedPreferences settings = getSharedPreferences("SolarInfo", 0);

		// If we have some data saved previously - get it
		// or fill with default values if not
		url = settings.getString("url",
				"http://informer.gismeteo.ru/xml/34519_1.xml");
		ip = settings.getString("IP", "10.40.30.91");
		port = settings.getInt("PORT", 3128);
		useProxy = settings.getBoolean("USEPROXY", true);

		cbUseProxy = (CheckBox) findViewById(R.id.useProxy);
		etURL = (EditText) findViewById(R.id.xmlURL);
		etIP = (EditText) findViewById(R.id.ipPart);
		etPort = (EditText) findViewById(R.id.ipPort);

		cbUseProxy.setChecked(useProxy);
		etURL.setText(url);
		etIP.setText(ip);
		etPort.setText(Integer.toString(port));
		
		setFieldsEnable(cbUseProxy.isChecked());
		
		Button returnButton = (Button) findViewById(R.id.returnButton);
		returnButton.setOnClickListener(onReturnButton);
		
		cbUseProxy.setOnClickListener(onUseProxy);
		cbUseProxy.setOnCheckedChangeListener(onUseProxy1);

	}

	OnClickListener onReturnButton = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			finish();
		}
	};
	
	OnClickListener onUseProxy = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			setFieldsEnable(cbUseProxy.isChecked());
			Toast.makeText(getApplicationContext(), "onClick", 1000);
			
		}
	};
	
	
	OnCheckedChangeListener onUseProxy1 = new OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			setFieldsEnable(isChecked);
			Toast.makeText(getApplicationContext(), "onClick", 1000);
			
		}
	};
	
	private void setFieldsEnable(boolean checked)
	{
		etIP.setEnabled(checked);
		etPort.setEnabled(checked);
	}

	@Override
	protected void onStop() {
		super.onStop();

		// Restore preferences
		SharedPreferences settings = getSharedPreferences("SolarInfo", 0);

		// Get settings editor
		SharedPreferences.Editor editor = settings.edit();

		editor.putString("url", etURL.getText().toString());
		editor.putString("IP", etIP.getText().toString());
		editor.putInt("PORT", Integer.parseInt(etPort.getText().toString()));
		editor.putBoolean("USEPROXY", cbUseProxy.isChecked());

		// Commit the edits!
		editor.commit();
	}

}
